package com.jeasonzhao.web;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.parser.expression.CSSParser;
import com.jeasonzhao.commons.basic.StringCollection;
import com.jeasonzhao.commons.utils.Guid;

public class ControlSupportTag extends BasicTagSupport
{
    private static final long serialVersionUID = 1L;
    private String cssName = null; //Name of the CSS class
    private String style = null; //CSS Style
    private String tagName = null; //Tag Name
    private java.util.Hashtable<String,String> temparyAttributes = new java.util.Hashtable<String,String>();
    public ControlSupportTag()
    {
        super();
    }

    public ControlSupportTag(String tagName)
    {
        super();
        this.tagName = tagName;
    }

    public String getCssName()
    {
        return cssName;
    }

    public String getStyle()
    {
        return style;
    }

    public String getTagName()
    {
        return tagName;
    }

    public void setCssName(String cssName)
    {
        this.cssName = cssName;
    }

    public void setStyle(String style)
    {
        this.style = style;
    }

    public void setTagName(String tagName)
    {
        this.tagName = tagName;
    }

    protected java.util.Hashtable<String,String> getAttributes()
    {
        return this.temparyAttributes;
    }

    protected void clearAttributes()
    {
        if(null != temparyAttributes)
        {
            temparyAttributes.clear();
        }
    }

    public ControlSupportTag setAttribute(String name,String value)
    {
        if(false == Algorithms.isEmpty(name))
        {
            name = name.trim();
            if(name.equalsIgnoreCase("id") || name.equalsIgnoreCase("name"))
            {
                this.setId(value);
            }
            else if(name.equalsIgnoreCase("class"))
            {
                this.setCssName(value);
            }
            else if(name.equalsIgnoreCase("style"))
            {
                this.setStyle(value);
            }
            else if(Algorithms.isEmpty(value))
            {
                temparyAttributes.remove(name);
            }
            else
            {
                temparyAttributes.put(name,value);
            }
        }
        return this;
    }

    protected static String refineCssNames(String strCss)
    {
        if(Algorithms.isEmpty(strCss))
        {
            return strCss;
        }
        else
        {
            StringCollection sc = StringCollection.splitString(strCss," ");
            sc.uniqueItems();
            return sc.toString(" ",false);
        }
    }

    protected void writeTagEndText()
    {
        if(Algorithms.isEmpty(tagName) == false)
        {
            write("</" + tagName.trim() + ">");
        }
    }

    protected void writeTagBeginText()
    {
        this.write(getTagBeginText(this.tagName,this.getId(),this));
    }

    protected static String getTagBeginText(String tagName,String id,ControlSupportTag t)
    {
        return getTagBeginText(tagName,id,null == t ? null : t.cssName,
                               null == t ? null : t.style,
                               null == t ? null : t.temparyAttributes);
    }
    protected static String getTagBeginText(String tagName,String id,String cssName,String style
                                            ,String... attributes)
    {
        java.util.Hashtable<String,String> a=new java.util.Hashtable<String,String>();
        for(int n=0;null!=attributes && n<attributes.length&& (n+1)<attributes.length;n+=2)
        {
            if(null!=attributes[n]&& null!=attributes[n+1])
            {
                a.put(attributes[n],attributes[n+1]);
            }
        }
        return getTagBeginText(tagName,id,cssName,style,a);
    }
    protected static String getTagBeginText(String tagName,String id,String cssName,String style
                                            ,java.util.Hashtable<String,String> attributes)
    {
        StringBuffer buf = new StringBuffer();
        if(Algorithms.isEmpty(tagName) == false)
        {
            buf.append("<" + tagName.trim());
            if(Algorithms.isEmpty(cssName) == false)
            {
//                debug("refineCssNames "+cssName+" = "+refineCssNames(cssName));
                buf.append(" class=\"" +refineCssNames(cssName) + "\"");
            }
            if(Algorithms.isEmpty(style) == false)
            {
//                debug("CSSParser.refinePart "+style+" = "+CSSParser.refinePart(style));
                buf.append(" style=\"" + CSSParser.refinePart(style) + "\"");
            }
            if(Algorithms.isEmpty(id) == false)
            {
                buf.append(" id=\"" + id.trim() + "\"");
                if("|input|form|select|option|textarea|button|".indexOf(tagName.toLowerCase()) >= 0)
                {
                    buf.append(" name=\"" + id.trim() + "\"");
                }
            }
            if(null != attributes)
            {
                for(java.util.Map.Entry<String,String> e: attributes.entrySet())
                {
                    buf.append(" " + e.getKey() + "=\"" + e.getValue() + "\"");
                }
            }
            buf.append(">");
        }
        return buf.toString();
    }

    public ControlSupportTag addCssStyle(String name,String value)
    {
        if(Algorithms.isEmpty(name) || Algorithms.isEmpty(value))
        {
            return this;
        }
        if(Algorithms.isEmpty(this.style))
        {
            this.style = "";
        }
        this.style += name.trim() + ":" + value.trim();
        return this;
    }

    public String generateDefaultId()
    {
        if(Algorithms.isEmpty(this.getId()))
        {
            this.setId(Guid.newGuid("DEFAULT_"));
        }
        return this.getId();
    }
}
